<template>
  <div class="app-container">
    <el-row :gutter="15">
      <el-col :span="12">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="阅读状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择公告状态" clearable size="small">
              <el-option
                v-for="dict in dict.type.sys_read_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:noticeUser:remove']"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="noticeUserList" @selection-change="handleSelectionChange" border stripe>
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column label="用户公告ID" align="center" prop="noticeUserId" v-if="true"/> -->
          <!-- <el-table-column label="公告ID" align="center" prop="noticeId" /> -->
          <el-table-column label="部门" align="center" prop="deptName" />
          <el-table-column label="姓名" align="center" prop="nickName" />
          <el-table-column label="公告状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_read_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:noticeUser:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
      <el-col :span="12">
        <el-descriptions class="margin-top" :column="2"  border>

            <el-descriptions-item label-class-name="my-label">
              <template slot="label">
                <i class="el-icon-tickets"></i>
                标题
              </template>
              {{form.noticeTitle}}
            </el-descriptions-item>
            <el-descriptions-item label-class-name="my-label">
              <template slot="label">
                <i class="el-icon-tickets"></i>
                类型
              </template>
<dict-tag :options="dict.type.sys_notice_type" :value="form.noticeType"/>
            </el-descriptions-item>
            <el-descriptions-item label-class-name="my-label">
              <template slot="label">
                <i class="el-icon-tickets"></i>
                内容
              </template>
              <div v-html="form.noticeContent"></div>
            </el-descriptions-item>
          </el-descriptions>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";
import { listNoticeUser, getNoticeUser, delNoticeUser, addNoticeUser, updateNoticeUser } from "@/api/system/noticeUser";

export default {
  name: "NoticeUser",
  dicts: ['sys_read_status', 'sys_notice_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告用户表格数据
      noticeUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeId: undefined,
        userId: undefined,
        status: undefined,
        noticeId:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const id = this.$router.currentRoute.params.objId;
    this.getList(id);
  },
  methods: {
    /** 查询公告用户列表 */
    getList(id) {
      this.reset();
      this.queryParams.noticeId = id;
      this.loading = true;
      this.reset();
      getNotice(id).then(response => {
        this.form = response.data;
      });
      listNoticeUser(this.queryParams).then(response => {
        this.noticeUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeUserId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公告用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const noticeUserId = row.noticeUserId || this.ids
      getNoticeUser(noticeUserId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改公告用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.noticeUserId != null) {
            updateNoticeUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addNoticeUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeUserIds = row.noticeUserId || this.ids;
      this.$modal.confirm('是否确认删除公告用户编号为"' + noticeUserIds + '"的数据项？').then(() => {
        this.loading = true;
        return delNoticeUser(noticeUserIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/noticeUser/export', {
        ...this.queryParams
      }, `noticeUser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.el-descriptions-item__label.is-bordered-label.my-label {
      width: 6rem;
  }
</style>
